﻿using System.Web;
using System.Collections.Specialized;
using System.Text;
using System;

namespace HelpersDotNet.Extensions
{
    public static partial class Extensions
    {
        /// <summary>
        /// Converts a <see cref="System.String"/> to an HTML-encoded string.
        /// </summary>
        /// <param name="str">The string to encode.</param>
        /// <returns></returns>
        public static string ToEncodedHtml(this string str)
        {
            return HttpUtility.HtmlEncode(str);
        }

        /// <summary>
        /// Converts a string that has been HTML-encoded for HTTP transmission into
        /// a decoded string.
        /// </summary>
        /// <param name="str">The string to decode.</param>
        /// <returns></returns>
        public static string ToDecodedHtml(this string str)
        {
            return HttpUtility.HtmlDecode(str);
        }

        /// <summary>
        /// Encodes a URL string.
        /// </summary>
        /// <param name="str">The string to encode.</param>
        /// <returns></returns>
        public static string ToEncodedUrl(this string str)
        {
            return HttpUtility.UrlEncode(str);
        }

        /// <summary>
        /// Converts a string that has been encoded for transmission in a URL into a decoded string.
        /// </summary>
        /// <param name="str">The string to decode.</param>
        public static string ToDecodedUrl(this string str)
        {
            return HttpUtility.UrlDecode(str);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str">Querystring to parse.</param>
        /// <returns></returns>
        public static NameValueCollection ParseQueryString(this string str)
        {
            return HttpUtility.ParseQueryString(str);
        }

        public static NameValueCollection ParseQueryString(this string str, Encoding encoding)
        {
            return HttpUtility.ParseQueryString(str, encoding);
        }

        public static float ToFloat(this string str)
        {
            return float.Parse(str);
        }

        public static int ToInt(this string str)
        {
            return int.Parse(str);
        }

        public static double ToDouble(this string str)
        {
            return double.Parse(str);
        }

        public static char ToChar(this string s)
        {
            return char.Parse(s);
        }

        public static DateTime ToDateTime(this string s)
        {
            return DateTime.Parse(s);
        }

        /// <summary>
        /// Converts the string representation of a number to <see cref="System.Decimal"/> equivalent.
        /// </summary>
        /// <param name="s">String representation of the number to convert.</param>
        /// <returns></returns>
        public static decimal ToDecimal(this string s)
        {
            return decimal.Parse(s);
        }
    }
}